﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;

namespace University.Workshop.WinApp
{
    public partial class CreateAnEditRole : Form
    {
        string nameRoleSelected;
        string userLogin;
        //string userCi = "";
        public CreateAnEditRole()
        {
            InitializeComponent();
            labelDate.Text = DateTime.Now.Date.ToShortDateString();
            userLogin = Main.userMenu[0]+" "+Main.userMenu[1];
            labelLogin.Text = userLogin;
            loadComboBoxRoles();
            loadComboBoxFunctions(nameRoleSelected);//""=Not role selected
            IssuePermits();
        }

        private void IssuePermits()
        {
            //buttonAddRole.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        }

        private void loadComboBoxFunctions(string nameRole)
        {
            comboBoxFunctions.Items.Clear();
            DataTable temporalData = new DataTable();
            temporalData = FunctionsManager.LoadDataFunctions(nameRole);
            for (int i = 0; i < temporalData.Rows.Count; i++)
            {
                comboBoxFunctions.Items.Add(temporalData.Rows[i].ItemArray[0]);
            }
        }

        private void loadComboBoxRoles()
        {
            comboBoxRoles.Items.Clear();
            DataTable temporalData = new DataTable();
            temporalData = FunctionsManager.LoadDataRoles();
            for (int i = 0; i < temporalData.Rows.Count; i++)
            {
                comboBoxRoles.Items.Add(temporalData.Rows[i].ItemArray[0]);
            }
        }

        private void loadListBoxFunctionsOfRoleSelected(string nameRole)
        {
            listBoxFunctionsOfRoleSelected.Items.Clear();
            DataTable temporalData = new DataTable();
            temporalData = FunctionsManager.LoadDataFunctionOfRole(nameRole);
            for (int i = 0; i < temporalData.Rows.Count; i++)
            {
                listBoxFunctionsOfRoleSelected.Items.Add(temporalData.Rows[i].ItemArray[0]);
            }
        }

        private void comboBoxRoles_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                nameRoleSelected = comboBoxRoles.SelectedItem.ToString();
                loadListBoxFunctionsOfRoleSelected(nameRoleSelected);
                loadComboBoxFunctions(nameRoleSelected);
            }
            catch (Exception)
            {
                
            }
        }

        private void buttonAddFunction_Click(object sender, EventArgs e)
        {
            if (comboBoxRoles.SelectedItem != null)
            {
                if (comboBoxFunctions.SelectedItem != null)
                {
                    if (FunctionsManager.AddFunction(comboBoxRoles.SelectedItem.ToString(),comboBoxFunctions.SelectedItem.ToString()))
                    {
                        MessageBox.Show("Added function successfully");
                        loadComboBoxFunctions(nameRoleSelected);
                        loadListBoxFunctionsOfRoleSelected(nameRoleSelected);
                        comboBoxFunctions.SelectedItem =null;
                        comboBoxFunctions.Text = "";
                    }
                    else
                    {
                        MessageBox.Show("Function not added");
                    }
                }
                else
                {
                    MessageBox.Show("You must first select a function");
                }
            }
            else
            {
                MessageBox.Show("You must first select a Role");
            }
        }

        private void buttonRemoveFunction_Click(object sender, EventArgs e)
        {
            if (comboBoxRoles.SelectedItem != null)
            {
                if (listBoxFunctionsOfRoleSelected.SelectedItem != null)
                {
                    DialogResult confirm = MessageBox.Show("Are you sure to delete the  function?", "Delete", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);
                    if (confirm.Equals(DialogResult.OK))
                    {
                        if (FunctionsManager.RemoveFunctionOfTheRole(comboBoxRoles.SelectedItem.ToString(), listBoxFunctionsOfRoleSelected.SelectedItem.ToString()))
                        {
                            MessageBox.Show("Delete function successfully");
                            loadComboBoxFunctions(nameRoleSelected);
                            loadListBoxFunctionsOfRoleSelected(nameRoleSelected);
                            comboBoxFunctions.SelectedItem = null;
                            comboBoxFunctions.Text = "";
                            listBoxFunctionsOfRoleSelected.SelectedItem = null;
                        }
                        else
                        {
                            MessageBox.Show("Function not eliminated");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("You must first select a function to delete");
                }
            }
            else
            {
                MessageBox.Show("You must first select a Role");
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonAddRole_Click(object sender, EventArgs e)
        {
            if (textBoxNewRole.Text != "")
            {
                if (notExistRole(textBoxNewRole.Text))
                {
                    if (FunctionsManager.AddRole(textBoxNewRole.Text))
                    {
                        MessageBox.Show("Create Role successfully");
                        comboBoxRoles.SelectedItem = null;
                        comboBoxRoles.Text = "";
                        loadComboBoxRoles();
                        nameRoleSelected = "";
                    }
                    else
                    {
                        MessageBox.Show("Role not created");
                    }
                }
                else
                {
                    MessageBox.Show("The role already exists");
                }                
            }
            else
            {
                MessageBox.Show("Must enter a role name");
            }
        }

        private bool notExistRole(string nameRole)
        {
            return FunctionsManager.LoadDataRoles(nameRole);
        }

        private void buttonRemoveRole_Click(object sender, EventArgs e)
        {
            if (comboBoxRoles.SelectedItem!=null)
            {
                if (FunctionsManager.DeleteRole(comboBoxRoles.SelectedItem.ToString()))
                {
                    MessageBox.Show("Delete Role successfully");
                    comboBoxRoles.SelectedItem = null;
                    comboBoxRoles.Text = "";
                    loadComboBoxRoles();
                    nameRoleSelected = "";
                }
                else
                {
                    MessageBox.Show("Role not Deleted");
                }
            }
            else
            {
                MessageBox.Show("First select a role ");
            }
        }

        private void CreateAnEditRole_FormClosed(object sender, FormClosedEventArgs e)
        {
            Singleton.Instance.valid = true;
        }
    }
}
